package com.yunshang.ysmall.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.mock.MockParameterMetaData.Parameter;
import com.mysql.fabric.xmlrpc.base.Array;
import com.yunshang.ysmall.model.OrderItemInfo;
import com.yunshang.ysmall.model.SupplierInfo;
import com.yunshang.ysmall.service.SupplierService;

@Controller
@RequestMapping("/supplier")
public class SupplierController {
	@Resource
	SupplierService supplierService;
	 
	//添加供货商信息视图显示
	@RequestMapping("/addSupplier")
	public String addSupplier(SupplierInfo supplierInfo){
		return "/mng/main/supplier/addsupplier";
	}
	//保存供货商信息
	@RequestMapping("/saveSupplier")
	public String saveSupplier(SupplierInfo supplierInfo){
		supplierService.saveSupplierInfo(supplierInfo);
		return "redirect:/supplier/addSupplier";
	}
	//查询所有供货商信息
	@RequestMapping("findSupplier")
	public String  findSUpplier(Model model){
		List<SupplierInfo> suppliers=supplierService.findSupplierAll();
		model.addAttribute("suppliers",suppliers);
		return "/mng/main/supplier/supplierlist";
	}
	
	
	//根据条件查询供应商信息
	@RequestMapping("findConditionQuery")
	public String  findConditionQuery(Model model,SupplierInfo supplierInfo){
		List<SupplierInfo> suppliers=supplierService.conditionQuerySupplier(supplierInfo);
		model.addAttribute("suppliers",suppliers);
		return "/mng/main/supplier/supplierlist";
	}
	
	//查询所有的订单详细信息
	@RequestMapping("/findItemsAll")
	public String findItemsAll(Model model){
		List<OrderItemInfo> orderItemInfos=supplierService.findOrderItemList();
		for (OrderItemInfo orderItemInfo : orderItemInfos) {
			SupplierInfo supplierInfo=supplierService.findSoleSupplier(orderItemInfo.getProductId());
			if(supplierInfo!=null){
				orderItemInfo.setSupplierInfo(supplierInfo);
			}
		}
		model.addAttribute("orderItemInfos", orderItemInfos);
		return "/mng/main/supplier/matchanorder";
	}
	
	//根据时间段来查询订单详细信息
	@RequestMapping("dateFindOrderItemInfo")
	public String  dateFindOrderItemInfo(String startLastUpdTs,String endLastUpdTs,Model model) throws ParseException{
		/*if(endLastUpdTs.indexOf("31")<0){
			SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
			Date date=dateFormat.parse(endLastUpdTs);
			Calendar calendar=Calendar.getInstance();
			calendar.setTime(date);
			//获取当月中的某一天
			calendar.add(calendar.DAY_OF_MONTH,1);
			endLastUpdTs=dateFormat.format(calendar.getTime());
			
		}
		*/
		
		Map<String, Object> parameter=new HashMap<String, Object>();
		parameter.put("startLastUpdTs",startLastUpdTs);
		parameter.put("endLastUpdTs",endLastUpdTs+" 23:59:59");
		List<OrderItemInfo> orderItemInfos=supplierService.findOrderItemInfos(parameter);
		if(orderItemInfos!=null){
			for (OrderItemInfo orderItemInfo : orderItemInfos) {
				SupplierInfo supplierInfo=supplierService.findSoleSupplier(orderItemInfo.getProductId());
				if(supplierInfo!=null){
					orderItemInfo.setSupplierInfo(supplierInfo);
				}
			}
		}
		model.addAttribute("orderItemInfos", orderItemInfos);
		return "/mng/main/supplier/matchanorder";
	}
	
}
